package com.atguigu.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description
 * @Author: TT
 * @Date: 2022/8/8 13:01
 */
@RestController
@RequestMapping("msg")
public class ProductController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";

    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message, @PathVariable Integer delayTime) {
           rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME,DELAYED_ROUTING_KEY,"你是真棒",correlationData->{
               correlationData.getMessageProperties().setDelay(delayTime);
               return correlationData;
           });
        //log.info("当前时间：{},发送一条延迟{}毫秒的信息给队列delayed.queue:{}", new Date(),delayTime, message);
        System.out.println("当前时间"+new Date()+"毫秒的信息给队列"+message);
    }

    @RabbitListener(queues = DELAYED_QUEUE_NAME)
    public void receiveDelayedQueue(Message message){
        String msg = new String(message.getBody());
        System.out.println(new Date().toString()+msg);
       // log.info("当前时间：{},收到延时队列的消息：{}", new Date().toString(), msg);
    }
}
